package com.dc.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dc.dao.ShopDao;
import com.dc.dao.impl.ShopDaoImpl;
import com.dc.service.ShopService;
import com.dc.service.impl.ShopServiceImpl;
import com.dc.tools.SendSms;
import com.google.gson.Gson;


/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/ShopLoginServlet")
public class ShopLoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	ShopService shopService = new ShopServiceImpl();
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// 起始位置设置请求编码
		request.setCharacterEncoding("utf-8");
		
		//为了区分不同的请求，设置一个临时变量，根据不同变量的值来执行不同的方法
		String op = ""; //赋值为""
		if (request.getParameter("op")!=null) {
			op = request.getParameter("op");
		}
		//用户登录
		if ("shop_login".equals(op)) {
			shopLogin(request, response);
		}else if ("ajaxSendmsg".equals(op)) {
			// 获取验证码
			doAjaxSendMsg(request, response);
		}else if ("ajaxUsername".equals(op)) {
			// 验证输入的手机号码是否已存在（ajax）
			//doAjaxUsername(request, response);
		}	
	}
	
	/**
	 * 生成手机验证码
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void doAjaxSendMsg(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 1.设置响应头的contentType
		response.setContentType("application/json;charset=utf-8");
		String shopphone = "";
		System.out.println(request.getParameter("shopphone"));
		if (request.getParameter("shopphone") != null) {	
			shopphone = request.getParameter("shopphone");
		}
		System.out.println(shopphone);
		// 2生成一个六位随机数
		Random random = new Random();
		String scode = "";
		for (int i = 0; i < 6; i++) {
			scode += random.nextInt(10);
		}
		System.out.println("scode=" + scode);
		SendSms.sendSms(scode, shopphone);
		Gson gson = new Gson();
		String jsonString = gson.toJson(scode);
		// 4 响应
		PrintWriter outPrintWriter = response.getWriter();
		//成功
		request.getRequestDispatcher("shop_manager.jsp").forward(request, response);
		outPrintWriter.print(jsonString);
		// 5.关闭
		outPrintWriter.close();
	}

	/**
	 * 商家登录
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void shopLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 获取表单的内容，使用request对象的方法request.getParameter("参数名");
		String shopname = request.getParameter("shopname");
		System.out.println("shopname:" + shopname);
		String shoppsw = request.getParameter("shoppsw");
		System.out.println("shoppsw:" + shoppsw);
		// 假设用户名为admin，密码为123456，实际上用户名和密码要去调用dao相关方法
		if (shopname != null) {
			ShopDao shopDao = new ShopDaoImpl();
			boolean flag = shopDao.doLogin(shopname, shoppsw);
			if (flag) {
				//将用户登录信息存储到session中
				request.getSession().setAttribute("shopname", shopname);
				
				//成功
				request.getRequestDispatcher("shop_manager.jsp").forward(request, response);
			} else {
				//失败
				response.sendRedirect("shop_login.jsp");
			}
		}else {
			System.out.println("用戶名不能为空！");
		}
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(req, resp);
	}

}
